<?php defined('SYSPATH') OR die('No direct access allowed.');
 
class mymenu_Core { 
	public static function get_nested_subpages($page, $selected=NULL, $numsteps=0, $step=0){
		$r='';
		$openones=$selected->get_openones();
	
		if($numsteps>0){
			if($step>=$numsteps){
				return;
			}
		}
			
		$r.= '<ul class="indented_menu">';
		foreach($page->where('visible',1)->children->as_array() as $child){
			$r.= '<li class="mp">';
			
			$linkclass='';
			if($child->id==$selected->id){
				$linkclass='selected';
			}
			$r.= html::anchor($child->shortname, $child->title , array('class'=>$linkclass));
			if(in_array($child->id, $openones)){
				$r.= mymenu::get_nested_subpages($child,$selected,  $numsteps, $step+1);
			}
			$r.= '</li>';	
		}
		$r.='</ul>';
		
		return $r;
	}
	
	public static function get_listed_subpages($page, $selected=NULL, $numsteps=0, $step=0){
		$r='';
		$openones=array_reverse($selected->get_openones());
		
		if($numsteps>0){
			if($step>=$numsteps){
				return;
			}
		}
		
		$count=0;
		
		
		foreach($openones as $openone){
			$page=ORM::factory('page',$openone);
			$childarray=$page->where('visible',1)->children->as_array();
			
			
			if(count($childarray)>0){
	
				if($count==count($openones)-1){
					$ulclass='open';
				}else{
					$ulclass='';
				}
				
				$r.= '<ul class="listed_menu '.$ulclass.'">';
								
				foreach($childarray as $child){
					
					$liclass='';
					$isopen=(in_array($child->id, $openones));
					
					if($isopen){
						$liclass='open';
					}
					$r.= '<li class="mp '.$liclass.'">';
					
					$linkclass='';
					if($isopen){
						$linkclass='selected';
					}
					$r.= html::anchor($child->shortname, $child->title , array('class'=>$linkclass));
					$r.= '</li>';	
				}
				$r.='</ul>';
				
			}
			$count++;
		}
		
		
		return $r;
	}
	
	
	
}